import os
import math
import random
import datetime
import argparse

import xlsxwriter


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-r', '--rows', type=int, default=36)
    parser.add_argument('-c', '--columns', type=int, default=4)
    parser.add_argument('-s', '--size', type=int, default=16)
    args = parser.parse_args()
    n, total = 0, args.rows * args.columns
    root = os.path.splitext(__file__)[0]
    os.makedirs(root, exist_ok=True)
    with xlsxwriter.Workbook(os.path.join(root, datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S.%f') + '.xlsx')) as workbook:
        worksheet = workbook.add_worksheet('题目')
        worksheet_answer = workbook.add_worksheet('答案')
        format = workbook.add_format({'font_size': args.size})
        for column in range(args.columns):
            column *= 2
            width = math.ceil(6 * args.size / 10)
            worksheet.set_column(column, column, width)
            worksheet_answer.set_column(column, column, width)
            worksheet_answer.set_column(column + 1, column + 1, math.ceil(2 * args.size / 10))
        while n < total:
            a, b = random.randint(10, 99), random.randint(10, 99)
            c = a - b
            if 0 < c < 100 and (a // 10 - b // 10) > c // 10:
                row, column = n // args.columns, n % args.columns
                column *= 2
                worksheet.write(row, column, f"{a}-{b}=", format)
                worksheet_answer.write(row, column, f"{a}-{b}=", format)
                worksheet_answer.write(row, column + 1, str(c), format)
                n += 1


if __name__ == '__main__':
    main()